/**
 * 冲借款弹出框前台逻辑 
 */

function externalInit(){
	var ds = getDataset('cjk_query_ds');
	ds.initialize();
	var row = ds.getEmptyRow();
	ds.addRow(row);
	ds.setRowSelected(0);
	$("bodyContainer").style.marginRight = "10px";
	billUI.eventHandler.queryBodyData();
	postInit();	
}
/**
 * 初始化
 * @return
 */
function postInit() {
	var jkDs = getDataset("jk_jkzb");
	var flag = true;
	//编辑时按钮处理状态
	billUI.setBillOperate(IBillOperate.OP_EDIT); 
	jkDs.onAfterRowSelect = function() {
		billUI.setBillOperate(IBillOperate.OP_EDIT); 
	}
	//数据改变时处理
	jkDs.onAfterDataChange = function(event, pageIndex)
	{	
		//zyx1设置是否选中，如果点击选中，则将冲借款金额 = 借款余额值
		var checkIndex = this.nameToIndex("zyx1");
		//zyx2设置 冲借款金额
		var cjkybjeIndex = this.nameToIndex("zyx2");
		var hkIndex = this.nameToIndex('hkybje');
		var row = jkDs.getRow(event.cellRowIndex);
		if(event.cellColIndex == checkIndex){
			if(event.currentValue == 'Y'){
				var ybye = row.getCellValue(jkDs.nameToIndex('ybye'));
				//将冲借款金额 = 借款余额值
				jkDs.setValueAt(event.cellRowIndex, cjkybjeIndex, ybye);
				parent.setPageParamMap(row.getCellValue(jkDs.nameToIndex('pk_jkbx')),'Y');//将选择的项的pk放入map
			}
			else{//冲借款金额设为空
				jkDs.setValueAt(event.cellRowIndex, cjkybjeIndex, 0);
				parent.setPageParamMap(row.getCellValue(jkDs.nameToIndex('pk_jkbx')),'N');//将选择的项的pk放入map
			}
			$dealJe();
		}
		else if(event.cellColIndex == cjkybjeIndex){
			var cjkybje = row.getCellValue(cjkybjeIndex);
			if(cjkybje == '' || cjkybje == null)
				cjkybje = 0;
			var row = event.currentRow;
			var ybye = row.getCellValue(jkDs.nameToIndex('ybye'));
			if(parseFloat(cjkybje) < 0 ){
				//冲借款金额不能小于零!
				showMessageDialog("${ml:trans('yer_cjk001')}");
				jkDs.setValueAt(event.cellRowIndex, cjkybjeIndex, event.oldValue);
				return;
			}
			if(parseFloat(cjkybje) > parseFloat(ybye)){
				//冲借款金额不能大于借款单余额!
				showMessageDialog("${ml:trans('yer_cjk002')}");
				jkDs.setValueAt(event.cellRowIndex, cjkybjeIndex, event.oldValue);
				return;
			}
			$dealJe();
		}else if(event.cellColIndex == hkIndex){
			var hkybje = row.getCellValue(hkIndex);
			if(hkybje == '' || hkybje == null)
				hkybje = 0;
			if(parseFloat(hkybje) < 0 ){
				//还款金额不能小于零!
				showMessageDialog("${ml:trans('yer_cjk003')}");
				jkDs.setValueAt(event.cellRowIndex, hkIndex, event.oldValue);
				return;
			}
		}
		this.clearState();
	}
	//$dealJe();
}

/*处理冲借款金额、还款原币金额、表头原币金额 之间关系*/
function $dealJe(){
	var ds = getDataset('jk_jkzb');
	var headDs = parent.getDataset('bx_bxzb');
	var ybje = parseFloat(headDs.getSelectedRow().getCellValue(headDs.nameToIndex('ybje')));
	if(ds != null){
		var count = ds.getRowCount();
		var hkybjeIndex = ds.nameToIndex('hkybje');
		var zyx1Index = ds.nameToIndex('zyx1');
		var cjkybjeIndex = ds.nameToIndex('zyx2');
		var cjk_sum = 0;
		//计算选中行的冲借款金额和
		for(var i=0; i < count; i++){
			var isCheck = ds.getRow(i).getCellValue(zyx1Index);
			//限额
			if(isCheck == 'Y')
				cjk_sum += parseFloat(ds.getRow(i).getCellValue(cjkybjeIndex));
		}
		cjk_sum = parseFloat(cjk_sum);//冲借款总计
		//原币金额大于等于冲借款总计
		if(ybje >= cjk_sum){
			for(var i=0; i < count; i++)
				//还款原币金额置0
				ds.setValueAt(i,hkybjeIndex,0);
		}else{
			var hkje_sum = parseFloat(cjk_sum) - parseFloat(ybje);//还款总计
			for(var i=0; i < count; i++){
				var isCheck = ds.getRow(i).getCellValue(zyx1Index);
				if(isCheck == 'N' || hkje_sum == 0){
					ds.setValueAt(i,hkybjeIndex,0);
					continue;
				}
				var je = ds.getRow(i).getCellValue(cjkybjeIndex);
				if(je == '' || je == null) 
					je = 0;
				var cjkje = parseFloat(je);
				if(cjkje >= hkje_sum){
					ds.setValueAt(i,hkybjeIndex,hkje_sum);
					hkje_sum = 0;
				}else{
					ds.setValueAt(i,hkybjeIndex,cjkje);
					hkje_sum = hkje_sum - cjkje;
				}
				
			}
			
		}
	}
	ds.clearState();
}

/*根据默认条件查询表体数据*/
ListEventHandler.prototype.queryBodyData = function() {
	var bodyDs = null;
	// 多表头则获取主表数据
	if(this.billUI.billController.pageType == BillController.MULTIHEAD)
		bodyDs = this.getMasterDs();
	else
		bodyDs = this.getNowSubDs();
			
	if(this.billUI.billController.bodyCondition != null)
		bodyDs.addReqParameter(IDatasetConstant.NORMAL_QUERY_CONDITiON, this.billUI.billController.bodyCondition);
	bodyDs.addReqParameter("CJK_PARAM_MAP" ,parent.$getParamMap());
	var loader = new DatasetLoader(bodyDs);
	loader.setKeyValue(Dataset.MASTER_KEY);
	loader.load();
	//loadDataset(bodyDs, Dataset.MASTER_KEY, null, null, null, null, null);
};

/**
 * 点击“确定”
 */
function confirm()
{
	var ds = getDataset('jk_jkzb');
	var dsRowCount = ds.getRowCount();
	var selboxIndex = ds.nameToIndex('zyx1');//复选框
	var rows = new Array();
	var ybye_sum = 0;//原币余额总计
	var cjkybje_sum = 0;//冲借款总计
	var hkybje_sum = 0;//还款总计
	
	var pkJkbxIndex = ds.nameToIndex('pk_jkbx');
	var ybyeIndex = ds.nameToIndex('ybye');
	var cjkybjeIndex = ds.nameToIndex('zyx2');
	var hkybjeIndex = ds.nameToIndex('hkybje');
	for(var i=0; i < dsRowCount;i++){
		var row = ds.getRow(i);
		//如果是选中行
		if(row.getCellValue(selboxIndex) == 'Y'){
			var pk_jkbx = row.getCellValue(pkJkbxIndex);
			var cjkybje = row.getCellValue(cjkybjeIndex);
			if(cjkybje == null || cjkybje == '')
				cjkybje = 0;
			
			var hkybje = row.getCellValue(hkybjeIndex);
			if(hkybje == null || hkybje == '')
				hkybje = 0;
			
			var ybye = row.getCellValue(ybyeIndex);
			if(ybye == null || ybye == '')
				ybye = 0;
			
			rows.push(row);
			cjkybje_sum += parseFloat(cjkybje);
			hkybje_sum += parseFloat(hkybje);
		}
	}
		
	if(!validateData(rows,cjkybje_sum,hkybje_sum))
		return;
	//如果是还款单据，设置主表的页签值
	if(parent.getParameter('type') == 'hk'){
		parent.setBxzbJe(cjkybje_sum, hkybje_sum);//设置主表金额
	}else{
		//设置财务页签金额
		parent.setBxFinJe(cjkybje_sum, hkybje_sum);
	}
	//冲借款金额为0、则清空冲销ds
	if(cjkybje_sum == 0){
		parent.clearContrastDs();
	}else{
		parent.setContrast(ds, rows);//设置冲销
	} 
	var headDs = parent.getDataset('bx_bxzb');
	if(headDs.reqParameterMap.get('CJK_TYPE') == 'unusual'){//如果是报销单保存时弹出的冲借款的情况，则点击确定后，报销单同时保存提交
		parent.billUI.eventHandler.onBoSave();
	}
	var zb_cjkybje = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('cjkybje')); 
	if(zb_cjkybje == 0 && cjkybje_sum == 0)
		parent.billUI.eventHandler.noCjk = true;
	else
		parent.billUI.eventHandler.noCjk = false;
	parent.setParam('Y');
	parent.hideConfirmDialog();
	parent.hideDialog("contrast_dialogs", false);
	
};

function validateData(rows,cjkybje_sum,hkybje_sum){
	if(!$isBzbmEqual(rows)){
		showMessageDialog("${ml:trans('yer_cjk004')}");
		return false;
	}
	var headDs = parent.getDataset('bx_bxzb');
	var bxje_sum = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('ybje'));//主表报销金额
	if(cjkybje_sum > parseFloat(bxje_sum)){//冲借款大于报销金额
		if(hkybje_sum != cjkybje_sum - parseFloat(bxje_sum)){
			showMessageDialog("${ml:trans('yer_cjk005')}");
			return false;
		}
	}else if(hkybje_sum > 0){
		showMessageDialog("${ml:trans('yer_cjk006')}");
		return false;
	}
	return true;
}

/**
 * 点击“取消” 
 */
function cancel()
{
	var ds = getDataset('jk_jkzb');
	var count = ds.getRowCount();
	var hkybjeIndex = ds.nameToIndex('hkybje');
	var zyx1Index = ds.nameToIndex('zyx1');
	var cjkybjeIndex = ds.nameToIndex('zyx2');
	var cjk_sum = 0;
	
	var cDs = parent.getDataset('bx_contrast');
	var map = new HashMap();
	var pk_jkdIndex = cDs.nameToIndex('pk_jkd');
	for(var k=0,len=cDs.getRowCount(); k<len; k++){
		var pk_jkd = cDs.getRow(k).getCellValue(pk_jkdIndex);
		map.put(pk_jkd, pk_jkd);	
	}
	//清除选择的
	var pk_jkbxIndex = ds.nameToIndex('pk_jkbx');
	for(var i=0; i < count; i++){
		var pk_jkbx = ds.getRow(i).getCellValue(pk_jkbxIndex);
		if(!map.containsKey(pk_jkbx)){
			ds.setValueAt(i,zyx1Index,'N');
			ds.setValueAt(i,hkybjeIndex,parseFloat(0));
			ds.setValueAt(i,cjkybjeIndex,0);
		}
	}
	ds.clearState();	
	var headDs = parent.getDataset('bx_bxzb');
	var zb_cjkybje = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('cjkybje')); 
	if(zb_cjkybje == 0)
		parent.billUI.eventHandler.noCjk = true;
	else
		parent.billUI.eventHandler.noCjk = false;
	parent.hideDialog("contrast_dialogs", false);
};

/**
 * 借款单中作上预冲销标志
 */
function setYcx(){
	
};
/**
 * 借款单号字段回写到报销单上的借款单号字段上
 */
function setBxField()
{
	
};
/**
 * 借款单和报销单币种是否一致
 */
function $isBzbmEqual(rows)
{ 
	var bxDs = parent.getDataset('bx_bxzb');
	var jkDs = getDataset('jk_jkzb');
	var bzbmBxIndex = bxDs.nameToIndex('bzbm');
	var bzbmJkIndex = jkDs.nameToIndex('bzbm');
	var bxBzbm = bxDs.getSelectedRow().getCellValue(bzbmBxIndex);
	for(var i=0,len = rows.length; i<len; i++){
		if(rows[i].getCellValue(bzbmJkIndex) != bxBzbm)
			return false;
	}
	return true;
};


/*查询*/
function query(){
	var queryds = getDataset('cjk_query_ds');
	var jkbxr = queryds.getRow(0).getCellValue(queryds.nameToIndex('jkbxr'));
	var startDate = queryds.getRow(0).getCellValue(queryds.nameToIndex('start_time'));
	var endDate = queryds.getRow(0).getCellValue(queryds.nameToIndex('end_time'));
	var sql = '';
	if(jkbxr != null && jkbxr != '')
		sql += "jkbxr = '" + jkbxr +"'";
	else{
		//请先选择借款人名称!
		showMessageDialog("${ml:trans('yer_cjk007')}");
		return;
	}
	if(startDate != null && startDate != ''){
		if(sql != '')
			sql += " and djrq >= '" + startDate +"'";
		else
			sql += " djrq >= '" + startDate +"'";
	}
	if(endDate != null && endDate != ''){
		if(sql != '')
			sql += " and djrq <='" + endDate + "'";
		else
			sql += " djrq <='" + endDate + "'";
	}
	if(startDate != null && startDate != '' && endDate != null && endDate != ''){
		if(startDate > endDate){
			//起始日期不能大于终止日期!
			showMessageDialog("${ml:trans('yer_cjk008')}");
			return;
		}
	}
	
	//记录查询过什么，以免重复查询
	/*
	if(query.queryRecord == null){
		query.queryRecord = new Object;
	}
	if(query.queryRecord[sql] != null)
		return;
	query.queryRecord[sql] = 1;
	*/
	
	var targetDs = getDataset('jk_jkzb');
	targetDs.reqParameterMap.clear();
	//冲借款参数Map
	targetDs.addReqParameter("CJK_PARAM_MAP" ,parent.$getParamMap());
	if(sql != null && sql != '')
		targetDs.addReqParameter(IDatasetConstant.QUERY_PARAM_VALUES ,sql);
	targetDs.addReqParameter(IDatasetConstant.FROM_QUERY_TEMPLATE, "false");
	//根据dataset中的reqParameter进行数据装载
	var loader = new DatasetLoader(targetDs);
	loader.setKeyValue(Dataset.MASTER_KEY);
	//设置回调函数
	loader.setReturnFunc(afterLoadData);
	var rows = targetDs.getRows();
	if(rows != null && rows.length > 0){
		var cloneRows = new Array;
		for(var j = 0,len = rows.length; j < len; j ++){
			cloneRows[j] = rows[j].clone();
		}
		loader.setReturnArgs([cloneRows]);
	}
	loader.load();
}
//数据装载回调函数
function afterLoadData(args, success){
	var ds = getDataset('jk_jkzb');
	var count = ds.getRowCount();
	var map = parent.window.PageParamMap; 
	if(map == null)
		return;
	var pkIndex = ds.nameToIndex('pk_jkbx');
	var checkIndex = ds.nameToIndex('zyx1');
	for(var i=0; i < count; i++){
		var pk_jkbx = ds.getRow(i).getCellValue(pkIndex);
		if(map.get(pk_jkbx) == 'Y')
			ds.setValueAt(i,checkIndex,'Y');
	}
	/*
	if(args != null){
		var nowRows = ds.getRows();
		for(var i = 0,len = args[0].length; i < len; i ++){
			var exist = false;
			if(nowRows != null){
				for(var j = 0,len1 = nowRows.length; j < len1; j ++){
					if(nowRows[j].getCellValue(0) == args[0][i].getCellValue(0)){
						exist = true;
						break;
					}
				}
			}
			if(!exist)
				ds.addRow(args[0][i], false);
		}
	}
	*/
	ds.clearState();
}

ReferenceTextComp.prototype.beforeOpenRefDialog = function()
{
	var param = "djlxbm=" + parent.getSessionAttribute("djlxbm");
	var pDs = parent.getDataset('bx_bxzb');
	var pk_corp = pDs.getSelectedRow().getCellValue(pDs.nameToIndex('pk_corp'));
	return param + "&pk_corp=" + pk_corp;
};